
package com.chessimo.engine;

import java.io.*;

public class Crafty {
    
    
      

    public static void crafty() {  
        try {  
            Process p = Runtime.getRuntime().exec("cmd /C c:/ChessimoWinSer/crafty-23.5-64bit.exe");  
            BufferedReader in = new BufferedReader(  
                                new InputStreamReader(p.getInputStream())); 
            
            OutputStream outputStream = p.getOutputStream(); //stream writing to process standard input.
            byte [] buffer = new byte[4096];    
            outputStream.write ("e4\n".getBytes()); outputStream.flush();

            
            
            
            String line = null;  
            while ((line = in.readLine()) != null) {  
                System.out.println(line);  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
 
    
    
}


/*
 * 
 * 
 * 
 * package d.g.boardgames.chess;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CraftyPositionAnalyser implements PositionAnalyser {

  private String craftyCommand = "crafty" ;
  private int maxWaitTime = 5;
  private int minDepth = 14;
  
  public CraftyPositionAnalyser() {
    maxWaitTime = 5;
    minDepth = 14;
  }
  
  public CraftyPositionAnalyser(int maxWaitTime, int minDepth) {
    this.maxWaitTime = maxWaitTime;
    this.minDepth = minDepth;
  }
  
  public AnalysisData analyse(ChessBoard chessBoard) {
    FENSerializer fenSerializer = new FENSerializer();
    AnalysisData analysisData = new AnalysisData();
    Process process = null;
        try {
          
            
            process = Runtime.getRuntime().exec (craftyCommand);
            InputStream inputStream = process.getInputStream(); //stream reading from process standard output.
            OutputStream outputStream = process.getOutputStream(); //stream writing to process standard input.
            byte [] buffer = new byte[4096];
            
            
            outputStream.write ("ponder off\n".getBytes()); outputStream.flush();
            outputStream.write ("log off\n".getBytes()); outputStream.flush();
            String fenString = fenSerializer.getString(chessBoard);
            outputStream.write (("setboard " + fenString + "\n").getBytes()); outputStream.flush();
            outputStream.write ("analyze\n".getBytes()); outputStream.flush();
            
            Pattern p = Pattern.compile ("(\\d*)-> *[^ ]* *([^ ]*)(.*)");
            String input = "";
            boolean waitMore = true; int waitCount = 0;
            while (inputStream.available() >= 0 && waitMore) {
              if (inputStream.available() == 0) {
                try {Thread.sleep(1000);} catch (InterruptedException ie) {}
                waitCount++;
                if (waitCount == maxWaitTime) {
                  waitMore = false;
                }
              } else {
                waitCount = 0;
                int readByte = inputStream.read();
                  input += (char)readByte;
                  if ((char)readByte == '\r' || (char)readByte == '\n') {
        
                    Matcher m = p.matcher(input);
                    if (m.find()) {
                      try {
                          analysisData.setWhiteScore(Float.parseFloat(m.group(2)));
                          analysisData.setBlackScore(-analysisData.getWhiteScore());
                          analysisData.setComment(m.group(3));
                      } catch (NumberFormatException nfe) {
                        if (m.group(2).contains("Mat")) {
                          analysisData.setComment(m.group(3));
                          break;
                        }                        
                      }
                        if (Integer.parseInt(m.group(1)) >= minDepth) {
                          break;
                        }
                    }
                    if (input.contains("book moves")) {
                      analysisData.setComment(input);
                    }
                    input = "";
                  }
              }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
          if (process != null) {
              process.destroy();
            }
        }
        
    return analysisData;
  }
  
  public void skipContents(InputStream inputStream) throws IOException {
        byte [] buffer = new byte[4096];
        int readBytes = inputStream.read(buffer,0, 1);
        while (inputStream.available() > 0) {
            readBytes = inputStream.read(buffer);
            try {Thread.sleep(100);} catch (InterruptedException ie) {}
        }
        return;
    }
  
  public String getCraftyCommand() {
    return craftyCommand;
  }
  
  public void setCraftyCommand(String craftyCommand) {
    this.craftyCommand = craftyCommand;
  }

  public int getMaxWaitTime() {
    return maxWaitTime;
  }

  public void setMaxWaitTime(int maxWaitTime) {
    this.maxWaitTime = maxWaitTime;
  }

  public int getMinDepth() {
    return minDepth;
  }

  public void setMinDepth(int minDepth) {
    this.minDepth = minDepth;
  }
}
* 
* 
 */